﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Form for the application spy messaging
    /// </summary>
    public partial class SpyMessaging : Form
    {
        /// <summary>
        /// OriginalImage carried here.
        /// </summary>
        private DImage originalImage;

        /// <summary>
        /// Boolean to determine if the message written is new or retrieved.
        /// </summary>
        private bool overwriteMessage = false;

        /// <summary>
        /// Carries last time before edit message length.
        /// </summary>
        private int messageLength = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="SpyMessaging"/> class.
        /// </summary>
        /// <param name="img">The image.</param>
        public SpyMessaging(DImage img)
        {
            this.InitializeComponent();
            this.originalImage = img;
            this.Text += " " + this.originalImage.Name;
            this.LblMaxChars.Text = HideString.CaclulateMaxStringLength((double)img.Width, (double)img.Height).ToString();
        }

        /// <summary>
        /// Handles the TextChanged event of the TxtMessage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TxtMessage_TextChanged(object sender, EventArgs e)
        {
            this.LblMaxChars.Text = (int.Parse(this.LblMaxChars.Text) - (this.TxtMessage.Text.Length - this.messageLength)).ToString();
            this.messageLength = this.TxtMessage.Text.Length;
            if (int.Parse(this.LblMaxChars.Text) < 0)
            {
                this.LblMaxChars.Text = "Image will be resized to fit the text document";
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnRetrieveMessage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnRetrieveMessage_Click(object sender, EventArgs e)
        {
            this.TxtMessage.Text = HideString.ReadString(this.originalImage.ImageStructArray, this.TxtPassword.Text);
            this.overwriteMessage = true;
        }

        /// <summary>
        /// Handles the Click event of the BtnSaveMessage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnSaveMessage_Click(object sender, EventArgs e)
        {
            if (this.TxtPassword.Text == string.Empty)
            {
                MessageBox.Show("Please enter a password");
                return;
            }

            if (this.TxtMessage.Text == string.Empty)
            {
                MessageBox.Show("Please enter a message.");
                return;
            }

            if (this.overwriteMessage)
            {
                DialogResult answer = MessageBox.Show("Are you sure you want to overwrite the message in the current image?", "Overwrite?", MessageBoxButtons.YesNo);
                if (answer == DialogResult.No)
                {
                    return;
                }
            }

            if (this.LblMaxChars.Text == "Image will be resized to fit the text document")
            {
                int newX = 0, newY = 0;
                HideString.CalculateNewSize(ref newX, ref newY, this.TxtMessage.Text.Length);
                ImageStructure[,] img = ResizeImage.BicubicResize(newX, newY, this.originalImage.ImageStructArray, 3);
                this.originalImage.UpdateImage(HideString.WriteString(img, this.TxtMessage.Text, this.TxtPassword.Text));
            }

            Main.OperationName = "Spy message added!";
            this.originalImage.UpdateImage(HideString.WriteString(this.originalImage.ImageStructArray, this.TxtMessage.Text, this.TxtPassword.Text));
            this.Close();
        }
    }
}
